package com.zjsru.product.service.impl;


import com.zjsru.product.mapper.ProductMapper;
import com.zjsru.product.pojo.Product;
import com.zjsru.product.service.ProductService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductMapper productMapper;
    @Override
    public void createCategory(Product product) {
        product.setCreateTime(LocalDateTime.now());
        product.setUpdateTime(LocalDateTime.now());
        productMapper.createCategory(product);
    }

    @Override
    public void deleteCategory(Integer productId) {
        productMapper.deleteCategory(productId);
    }

    @Override
    public List<Product> searchCategory(String productName) {
        if (productName==null|| productName.isEmpty()){
            List<Product> products = productMapper.searchCategory();
            return products;
        }else {
            List<Product> products = productMapper.searchCategoryByName(productName);
            return products;
        }
    }

    @Override
    public void updateCategory(Product product) {
        if (product==null){
            throw new RuntimeException("参数错误");
        }
        product.setUpdateTime(LocalDateTime.now());
        productMapper.updateCategory(product);
    }

    @Override
    public void updateProductCount(Integer integer, Long productId) {
        if (productId<0){
            throw new RuntimeException("参数错误");
        }
        if (integer<0){
            Integer i = productMapper.searchProductNum(productId);
            if (-integer>i){
                throw new RuntimeException("库存不足");
            }
        }
        LocalDateTime now = LocalDateTime.now();
        productMapper.addProductCount(integer, productId, now);
    }

    @Override
    public Product queryProductById(Long productId) {
        if (productId<0){
            throw new RuntimeException("参数错误");
        }
        return productMapper.queryProductById(productId);
    }


}
